import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { CreateTokenDto } from './dto/create-token.dto';
import { LoginService } from './login.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
    private loginService: LoginService,
    public router: Router
  ) {
    this.loginForm = this.formBuilder.group({
      account: 'alan',
      password: 'alan'
    } as CreateTokenDto);
  }

  ngOnInit() {}

  onSubmit(createTokenDto: CreateTokenDto) {
    this.loginService.login(createTokenDto).subscribe(authPostDto => {
      this.loginService.setToken(authPostDto.token);
      this.router.navigateByUrl('/');
    });
  }
}
